<template>
  <div class="app-container">
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" />
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :xs="9" :sm="6" :md="5" :lg="4" :xl="3">
        <div class="head-container">
          <el-input v-model="deptName" clearable placeholder="输入部门名称搜索" prefix-icon="el-icon-search" style="width: 100%;" class="filter-item" @input="getDeptDatas" />
        </div>
        <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false" default-expand-all @node-click="handleNodeClick" />
      </el-col>
      <!--用户数据-->
      <el-col :xs="15" :sm="18" :md="19" :lg="20" :xl="21">
        <!--工具栏-->
        <div class="head-container">
          <!-- 搜索 -->
          <el-input v-model="queryParams.blurry" clearable placeholder="输入名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
          <el-select v-model="queryParams.enabled" clearable placeholder="状态" class="filter-item" style="width: 90px" @change="toQuery">
            <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
          <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>

          <!-- 新增 -->
          <el-button v-permission="['AddUser']" class="filter-item" size="mini" type="primary" icon="el-icon-plus" style="margin-left:0px;" @click="add">新增</el-button>
        </div>
        <!--表格渲染-->
        <el-table v-loading="loading" :data="tableData" size="small" style="width: 100%;">
          <el-table-column prop="nickName" label="用户名" />
          <el-table-column prop="phone" label="电话" />
          <el-table-column :show-overflow-tooltip="true" prop="email" label="邮箱" />
          <!-- <el-table-column label="部门 / 岗位">
            <template slot-scope="scope">
              <div>{{ scope.row.dept.name }} / {{ scope.row.job.name }}</div>
            </template>
          </el-table-column> -->
          <!-- <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.enabled"
                active-color="#409EFF"
                inactive-color="#F56C6C"
                @change="changeEnabled(scope.row, scope.row.enabled,)"
              />
            </template>
          </el-table-column> -->
          <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期">
            <template slot-scope="scope">
              <span>{{ scope.row.createDate }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="checkPermission(['EditUser','DeleteUser'])" label="操作" width="125" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button v-permission="['EditUser']" size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)" />
              <el-popover
                :ref="scope.row.id"
                placement="top"
                width="180"
              >
                <p>确定删除本条数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                  <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                </div>
                <el-button slot="reference" v-permission="['DeleteUser']" type="danger" icon="el-icon-delete" size="mini" />
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination
          style="margin-top: 10px"
          :total="pagination.total"
          :current-page="pagination.page"
          align="center"
          layout="total, prev, pager, next, sizes"
          @size-change="pagination.size"
          @current-change="pageChange"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { getDepartmentTree } from '@/api/system/department'
import { getUsers, removeUser } from '@/api/system/user'
import eForm from './form'
export default {
  name: 'User',
  components: { eForm },
  mixins: [initData],
  data() {
    return {
      deptName: '',
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      initMethod: 'loadData',
      enabledTypeOptions: [
        { key: 'true', display_name: '激活' },
        { key: 'false', display_name: '锁定' }
      ],
      tableData: [],
      isAdd: false,
      delLoading: false
    }
  },
  created() {
    this.init()
    this.getDeptDatas()
  },
  methods: {
    checkPermission,
    loadData() {
      if (this.deptId) {
        this.queryParams.deptId = this.deptId
      }
      getUsers(this.queryParams).then(res => {
        const data = res.data
        this.tableData = data.list
        this.pagination.total = data.total
      }).finally(() => {
        this.loading = false
      })
    },
    getDeptDatas() {
      const params = {}
      if (this.deptName) { params['deptName'] = this.deptName }
      getDepartmentTree(params).then(res => {
        if (this.deptName) {
          this.depts = res.data
        } else {
          this.depts = []
          const department = { id: 0, name: '顶级部门', children: [] }
          department.children = res.data
          this.depts.push(department)
        }
      })
    },
    handleNodeClick(data) {
      if (data.id === 0) {
        this.deptId = null
      } else {
        this.deptId = data.id
      }
      this.init()
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = { ...data }
      _this.dialog = true
    },
    changeEnabled() {},
    subDelete(id) {
      this.delLoading = true
      removeUser(id).then(res => {
        this.loadData()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).finally(() => {
        this.delLoading = false
        this.$refs[id].doClose()
      })
    }
  }
}
</script>

<style scoped>
</style>
